package com.merchant.manage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.merchant.constant.Constants;
import com.merchant.json.ReadJson;
import com.merchant.util.LoadingDialog;
import com.umeng.analytics.MobclickAgent;
import com.zjxfood.merchant.activity.R;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;

public class ManageFoodsOrederDetail extends Activity implements
		OnClickListener {
	private TextView text_title;// 标题
	private ListView lv;
	private List<Map<String, Object>> mlistitem;
	private TextView foods_oreder_detail_text1;// 订单号
	private TextView foods_oreder_detail_text2;// 预订人姓名
	private TextView foods_oreder_detail_text3;// 预订人电话
	private TextView foods_oreder_detail_text4;// 就餐人数
	private TextView foods_oreder_detail_text5;// 就餐时间
	private TextView foods_oreder_detail_text6;// 总价
	private TextView foods_oreder_detail_text7;// 预付定金
	private TextView foods_oreder_detail_text8;// 付款时间
	private Button foods_order_btn;
	private ImageView manage_title_back_image;// 返回
	String str = null;
	private HashMap<String, Object> mUserMapLists;
	private HashMap<String, Object> mDataList;
	private ArrayList<HashMap<String, Object>> DataList;
	private ArrayList<HashMap<String, Object>> addDataList;
	private MyAdapter adapter;
	private String page = "1";
	private int lastVisibleIndex;
	private int x = 1;
	private RunTask mRunTask;
	private LoadingDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_foods_order_detail);
		Bundle bundle = getIntent().getExtras();
		str = bundle.getString("orderid");
		new Thread(GetOrderInfoByOrderId).start();
		dialog = new LoadingDialog(this);
		dialog.show();
		mRunTask = new RunTask();
		mRunTask.execute("");
		init();
	}

	private void init() {
		text_title = (TextView) findViewById(R.id.manage_title_text);
		text_title.setText("预定订单详情");
		lv = (ListView) findViewById(R.id.foods_order_detail_listview);
		foods_oreder_detail_text1 = (TextView) findViewById(R.id.foods_oreder_detail_text1);
		foods_oreder_detail_text2 = (TextView) findViewById(R.id.foods_oreder_detail_text2);
		foods_oreder_detail_text3 = (TextView) findViewById(R.id.foods_oreder_detail_text3);
		foods_oreder_detail_text4 = (TextView) findViewById(R.id.foods_oreder_detail_text4);
		foods_oreder_detail_text5 = (TextView) findViewById(R.id.foods_oreder_detail_text5);
		foods_oreder_detail_text6 = (TextView) findViewById(R.id.foods_oreder_detail_text6);
		foods_oreder_detail_text7 = (TextView) findViewById(R.id.foods_oreder_detail_text7);
		foods_oreder_detail_text8 = (TextView) findViewById(R.id.foods_oreder_detail_text8);
		foods_order_btn = (Button) findViewById(R.id.foods_order_btn);
		manage_title_back_image = (ImageView) findViewById(R.id.manage_title_back_image);
		manage_title_back_image.setOnClickListener(this);
		lv.setOnScrollListener(mScrollListener);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.manage_title_back_image:
			finish();
			break;
		}
	}

	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	// private List<Map<String, Object>> getData() {
	// List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	//
	// for (int i = 0; i < 10; i++) {
	// HashMap<String, Object> map = new HashMap<String, Object>();
	// // String url = "http://10.167.12.184:8080/examples/image/1.png";
	// //
	// // Bitmap bitmap = getImageByURL(url);
	//
	// // map.put("user_reviews_imageview", R.drawable.photo3);
	// map.put("user_reviews_text1", "香辣小龙虾");
	// map.put("user_reviews_text2", "x7");
	// map.put("user_reviews_text3", "¥25");
	// // map.put("user_reviews_button", "回复");
	// list.add(map);
	// }
	// return list;
	//
	// }

	Runnable GetOrderInfoByOrderId = new Runnable() {

		@Override
		public void run() {
			try {
				String ss = "orderId=" + str;
				String sign = Constants.sortsStr(ss);
				String strr = Constants.GetOrderInfoByOrderId + sign
						+ "&orderId=" + str;
				String json = ReadJson.readParse(strr);
				mUserMapLists = Constants.getJson2Object(json);
				mDataList = Constants.getJson2Object(mUserMapLists.get("Data")
						.toString());
				Log.i("mUserMap", "================" + mDataList);
				handler.sendEmptyMessageDelayed(1, 0);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};

	class RunTask extends AsyncTask<String, Integer, String> {

		@Override
		protected String doInBackground(String... strs) {
			switch (x) {
			case 1:
				try {
					String ss = "orderId=" + str + "&pageSize=" + 10
							+ "&currentPage=" + page;
					String sign = Constants.sortsStr(ss);
					String strr = Constants.GetOrderDetailList + sign
							+ "&orderId=" + str + "&pageSize=" + 10
							+ "&currentPage=" + page;
					String json = ReadJson.readParse(strr);
					// Log.i("sign", "================" + json);
					mUserMapLists = Constants.getJson2Object(json);
					DataList = Constants.getJsonArray(mUserMapLists.get("Data")
							.toString());
					// mMap = Constants.getJsonArray(json);
					handler.sendEmptyMessageDelayed(4, 0);
					Log.i("mDataList", "================" + DataList);
					dialog.dismiss();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					dialog.dismiss();
					handler.sendEmptyMessageDelayed(3, 0);
					e.printStackTrace();
				}
				break;

			case 2:
				String ss = "orderId=" + str + "&pageSize=" + 10
						+ "&currentPage=" + page;
				String sign = Constants.sortsStr(ss);
				String strr = Constants.GetOrderDetailList + sign + "&orderId="
						+ str + "&pageSize=" + 10 + "&currentPage=" + page;
				String json;

				try {
					json = ReadJson.readParse(strr);
					mUserMapLists = Constants.getJson2Object(json);
					addDataList = Constants.getJsonArray(mUserMapLists.get(
							"Data").toString());
					dialog.dismiss();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					dialog.dismiss();
					handler.sendEmptyMessageDelayed(3, 0);
					e.printStackTrace();
				}
				handler.sendEmptyMessageDelayed(5, 0);
				break;
			}

			return null;
		}
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				if (mDataList.get("orderid") != null) {
					foods_oreder_detail_text1.setText(mDataList.get("orderid")
							.toString());
				}
				// foods_oreder_detail_text2.setText(mDataList.get("orderid")
				// .toString());
				if (mDataList.get("mobile") != null) {
					foods_oreder_detail_text3.setText(mDataList.get("mobile")
							.toString());
				}
				if (mDataList.get("personsNum") != null) {
					foods_oreder_detail_text4.setText(mDataList.get(
							"personsNum").toString());
				}
				if (mDataList.get("createtime") != null) {
					foods_oreder_detail_text5.setText(mDataList.get(
							"createtime").toString());
				}
				if (mDataList.get("allcost") != null) {
					foods_oreder_detail_text6.setText("¥"
							+ mDataList.get("allcost").toString());
				}
				if (mDataList.get("deposit") != null) {
					foods_oreder_detail_text7.setText("¥"
							+ mDataList.get("deposit").toString());
				}
				if (mDataList.get("paytime") != null) {
					foods_oreder_detail_text8.setText(mDataList.get("paytime")
							.toString());
				}

				break;
			case 2:

				break;
			case 3:
				Toast.makeText(getApplicationContext(), "请检查您的网络链接!",
						Toast.LENGTH_SHORT).show();
				break;
			case 4:
				adapter = new MyAdapter(getApplicationContext(), DataList);
				lv.setAdapter(adapter);
				lv.setVisibility(View.VISIBLE);
				break;
			case 5:
				adapter.nofity(addDataList);
				break;
			}
		};
	};

	OnScrollListener mScrollListener = new OnScrollListener() {

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
					&& lastVisibleIndex == adapter.getCount() - 1) {
				x = 2;
				// lv.addFooterView(mFooterView);// 添加 加载更多
				page = Integer.parseInt(page) + 1 + "";
				mRunTask = new RunTask();
				mRunTask.execute("");
			}
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			// 计算最后可见条目的索引
			lastVisibleIndex = firstVisibleItem + visibleItemCount - 1;
		}
	};

	public final class ViewHolder {
		public TextView user_reviews_text1;
		public TextView user_reviews_text2;
		public TextView user_reviews_text3;
	}

	public class MyAdapter extends BaseAdapter {

		private LayoutInflater flater;
		private ArrayList<HashMap<String, Object>> mList;

		public MyAdapter(Context context,
				ArrayList<HashMap<String, Object>> list) {
			this.flater = LayoutInflater.from(context);
			this.mList = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		public void nofity(ArrayList<HashMap<String, Object>> list) {
			this.mList.addAll(list);
			notifyDataSetChanged();
		}

		@Override
		public View getView(int position, View converView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder hodler = null;
			if (converView == null) {
				hodler = new ViewHolder();
				converView = flater.inflate(R.layout.layout_yddc_items, null);

				hodler.user_reviews_text1 = (TextView) converView
						.findViewById(R.id.dc_name);
				hodler.user_reviews_text2 = (TextView) converView
						.findViewById(R.id.dc_number);
				hodler.user_reviews_text3 = (TextView) converView
						.findViewById(R.id.dc_money);

				converView.setTag(hodler);
			} else {
				hodler = (ViewHolder) converView.getTag();
			}

			hodler.user_reviews_text1.setText(mList.get(position).get("name")
					.toString());
			hodler.user_reviews_text2.setText("x"
					+ mList.get(position).get("amount").toString());
			hodler.user_reviews_text3.setText("¥"
					+ mList.get(position).get("cost").toString());
			return converView;
		}
	}
}
